package com.ruoyi.xuexiao;
 
import org.locationtech.jts.geom.Coordinate;
import org.geotools.geometry.jts.JTS;
 
import org.geotools.referencing.CRS;
 
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
 
import java.math.BigDecimal;
import java.text.DecimalFormat;
 
public class MyGeotools {
    public static void main(String[] args) {
        //某点的度分秒格式的经纬度，如果经纬度本身就是double格式的，就不需要再行计算
        Double y = Double.parseDouble(getPosition("32°11′38.977458″"));//纬度
        Double x = Double.parseDouble(getPosition("119°31′06.853758″"));//经度
        double s = 4322707.336439;
        double s1 = 38616767.05775486;
        Double[] coordinate = getCoordinate(s, s1);
        System.out.println("Y："+coordinate[0] + " " + "X："+coordinate[1]);
 
    }
 
    //坐标转换
    public static Double[] getCoordinate(Double x, Double y) {
        Double[] res = new Double[2];
        Coordinate tar = null;
        try {
            //封装点，这个是通用的，也可以用POINT（y,x）
            // private static WKTReader reader = new WKTReader( geometryFactory );
            Coordinate sour = new Coordinate(y, x);
            //这里要选择转换的坐标系是可以随意更换的
            CoordinateReferenceSystem source = CRS.decode("EPSG:4326");
            CoordinateReferenceSystem target = CRS.decode("EPSG:4549");
            //建立转换，下面两个我屏掉的转换方式会报出需要3/7参数的异常
            // MathTransform mathTransform = CRS.findMathTransform(source, target);
            //MathTransform mathTransform1 = CRS.findMathTransform(source, target, false);
            MathTransform transform = CRS.findMathTransform(source, target, true);
            tar = new Coordinate();
            //转换
            JTS.transform(sour, tar, transform);
        } catch (FactoryException | org.opengis.referencing.operation.TransformException e) {
            e.printStackTrace();
        }
        String[] split = (tar.toString().substring(1, tar.toString().length() - 1)).split(",");
        //经纬度精度
        DecimalFormat fm = new DecimalFormat("0.0000000");
        res[0] = Double.valueOf(fm.format(Double.valueOf(split[0])));
        res[1] = Double.valueOf(fm.format(Double.valueOf(split[1])));
        return res;
    }
 
    private static String getPosition(String position){
        String[] degree = position.split("\\°");
        if(degree.length == 1){
            return position;
        }
        String d = degree[0];
        String[] one = degree[1].split("\\′");
        String a = one[0];
        if(!a.contains(".")){
            if(a.substring(0,1).equals("0")){
                a = a.substring(1);
            }
        }else if(a.contains(".") && a.split("\\.").length > 1){
            a = a.substring(1);
        }
        String[] two = one[1].split("\\″");
        String b= two[0];
        if(!b.contains(".")){
            if(b.substring(0,1).equals("0")){
                b = b.substring(1);
            }
        }else if(b.contains(".") && b.split("\\.").length > 1){
            if(b.substring(0,1).equals("0")){
                b = b.substring(1);
            }
        }
        BigDecimal fen = new BigDecimal(a);
        BigDecimal miao = new BigDecimal(b);
        BigDecimal du = new BigDecimal(d);
//		float f = Float.valueOf(a)+ Float.valueOf(Float.valueOf(b)/60);
//		float du = Float.valueOf(f/60)+Float.valueOf(d);
        BigDecimal add = fen.add(miao.divide(new BigDecimal("60"),6, BigDecimal.ROUND_HALF_UP)).divide(new BigDecimal("60"),6, BigDecimal.ROUND_HALF_UP).add(du);
        return String.valueOf(add);
    }
}